package data

import (
	"context"
	"gitee.com/min65535/gotechback/techback/biz"
	models "gitee.com/min65535/gotechback/techback/data/orm"
	log "github.com/sirupsen/logrus"
	"gorm.io/gorm/clause"
)

type etxRepo struct {
	data *Data
}

func (e *etxRepo) SaveCtx(ctx context.Context, email string) error {
	query := e.data.DbSql.Model(&models.CustomEmails{}).Clauses(clause.OnConflict{
		Columns: []clause.Column{{Name: "email"}},
	}).Create(&models.CustomEmails{Email: email})

	if query.Error != nil {
		log.Errorf("etxRepo#etxRepo err:%v", query.Error)
		return query.Error
	}
	if query.RowsAffected <= 0 {
		log.Warningln("no data affected")
		return nil
	}
	return nil
}

func NewEmailCtxRepo(data *Data) biz.EmailCtxRepo {
	return &etxRepo{
		data: data,
	}
}
